package com.bayleaf.web.prefix;

import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;

import com.bayleaf.common.StringUtil;
import com.bayleaf.core.BayleafServlet;
import com.bayleaf.core.Model;


@WebServlet(name="prefix",
        urlPatterns={"/prefix"},
        initParams={ @WebInitParam(name="title", value="Adding Prefix, Postfix"), @WebInitParam(name="jsp", value="/prefix/Prefix") } )
public class PrefixServlet extends BayleafServlet {
	
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;


	@Override
    public String process(Model model) throws Exception{
    	
    	String input = model.getString("input");
    	String prefix = model.getString("prefix");
    	String postfix = model.getString("postfix");
    	
		model.setString("output", addingPrePostfix(StringUtil.strTokenizer(input), prefix, postfix));
	
        return null;
    }
    
    
    /**
     * 해당 필드 앞뒤에 prefix및 postfix를 붙인다
     * @param fields
     * @param prefix
     * @param postfix
     * @return prefix string postfix
     */
    public String addingPrePostfix(String[] fields, String prefix, String postfix){
//    	System.out.println(Arrays.toString(fields));
    	String result = "";
    	for(int i=0; i < fields.length; i ++){
    		result += prefix + fields[i] + postfix +System.getProperty( "line.separator" ); 
    	}
//    	System.out.println(result);
    	return result;
    }
    
}
